package com.kyist.frp.utils;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;

/**
 * 文件工具
 */
public class FileUtils {

    /**
     * 保存文件
     * @param path 路径
     * @param fileName 文件名
     * @param source 资源
     * @return 文件名
     */
    public static String saveFile(String path, String fileName,String source){
        File file = new File(path);
        if(!file.exists()){
            System.out.println(file.mkdirs());
        }
        try (
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(path+fileName), StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw, 2048)
        ){
            bw.write(source);
            return fileName;
        } catch (Exception e) {
            System.out.println("文件存储失败");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存文件
     * @param file 文件
     * @param source 资源
     * @return 文件名
     */
    public static String saveFile(String file,String source){
        try (
                OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw, 2048)
        ){
            bw.write(source);
            return file;
        } catch (Exception e) {
            System.out.println("文件存储失败");
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 读取指定路径文件，返回字符串
     * @param filePath 文件路径
     * @return str
     */
    public static String readAimFile(String filePath){
        StringBuilder builder = new StringBuilder();
        File file = new File(filePath);
        try(
                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        ){
            String buf = null;
            while ((buf = reader.readLine()) != null){
                builder.append(buf);
            }
        } catch (IOException e) {
            System.out.println("文件读取出错。。。\n" + e);
        }
        return builder.toString();
    }

    /**
     * 读取指定路径文件，返回字符串
     * @param filePath 文件路径
     * @return str
     */
    public static String readAimFileByLock(String filePath){
        StringBuilder builder = new StringBuilder();
        File file = new File(filePath);
        FileInputStream fis = null;
        FileChannel channel = null;
        FileLock fileLock = null;
        try{
            //文件输入流
            fis = new FileInputStream(file);
            //尝试获取文件锁
            channel = fis.getChannel();
            fileLock = channel.tryLock(0, 100, true);

            //获得锁开始读取文件
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while ( (channel.read(buffer)) > 0){
                builder.append(new String(buffer.array(), StandardCharsets.UTF_8));
                buffer.clear();
            }
        } catch (IOException e) {
            System.out.println("文件读取出错。。。\n" + e);
        }finally {

            try {
                if (fileLock != null){
                    fileLock.release();
                    fileLock.close();
                }
                if (channel != null){
                    channel.close();
                }
                if (fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return builder.toString();
    }

}
